package com.thb.pay.payment.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.api.payment.PayChannelTypeServiceApi;
import com.thb.pay.api.payment.vo.req.ChannelConfigTypePageReq;
import com.thb.pay.api.payment.vo.req.ChannelConfigTypeReq;
import com.thb.pay.api.payment.vo.resp.PayChannelTypeVo;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.entity.SuperEntity;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.payment.entity.PayChannelType;
import com.thb.pay.payment.service.PayChannelTypeService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 渠道类型表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@RestController
public class PayPayChannelTypeController implements PayChannelTypeServiceApi {

    @Autowired
    private PayChannelTypeService payChannelTypeService;

    @Override
    public PageResult<PayChannelTypeVo> findPage(@RequestBody ChannelConfigTypePageReq pageReq) {
        IPage<PayChannelType> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<PayChannelType> pageResult = payChannelTypeService.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, PayChannelTypeVo.class);
    }

    @Override
    public List<PayChannelTypeVo> findAllList() {
        List<PayChannelTypeVo> resultDataList = Lists.newArrayList();
        List<PayChannelType> payChannelTypeList = payChannelTypeService.selectList(null);
        payChannelTypeList.forEach(channelConfigType -> {
            PayChannelTypeVo resultData = new PayChannelTypeVo();
            BeanUtil.copyProperties(channelConfigType, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public List<PayChannelTypeVo> findListByIds(@RequestBody List<Long> ids) {
        List<PayChannelTypeVo> resultDataList = Lists.newArrayList();
        LambdaQueryWrapper<PayChannelType> wrapper = new LambdaQueryWrapper();
        wrapper.in(PayChannelType::getId, ids);
        List<PayChannelType> payChannelTypeList = payChannelTypeService.selectList(wrapper);
        payChannelTypeList.forEach(channelConfigType -> {
            PayChannelTypeVo resultData = new PayChannelTypeVo();
            BeanUtil.copyProperties(channelConfigType, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public PayChannelTypeVo getById(@RequestParam("id") Long id) {
        PayChannelType payChannelType = payChannelTypeService.selectById(id);
        if (payChannelType != null) {
            PayChannelTypeVo resultData = new PayChannelTypeVo();
            BeanUtil.copyProperties(payChannelType, resultData);
            return resultData;
        }
        return null;
    }

    @Override
    public PayChannelTypeVo getByCode(@RequestParam("code") String code) {
        LambdaQueryWrapper<PayChannelType> wrapper = new LambdaQueryWrapper();
        wrapper.eq(PayChannelType::getCode, code);
        List<PayChannelType> payChannelTypeList = payChannelTypeService.selectList(wrapper);
        if (CollUtil.isNotEmpty(payChannelTypeList)) {
            PayChannelType payChannelType = payChannelTypeList.get(0);
            PayChannelTypeVo resultData = new PayChannelTypeVo();
            BeanUtil.copyProperties(payChannelType, resultData);
            return resultData;
        }
        return null;
    }

    @Override
    public void add(@RequestBody ChannelConfigTypeReq channelConfigTypeReq) {
        PayChannelTypeVo payChannelTypeVo = getByCode(channelConfigTypeReq.getCode());
        if (payChannelTypeVo != null) {
            throw RRException.build("payChannelType.code.exist");
        }
        PayChannelType payChannelType = new PayChannelType();
        BeanUtil.copyProperties(channelConfigTypeReq, payChannelType);
        payChannelTypeService.insert(payChannelType);
    }

    @Override
    public void update(@RequestBody ChannelConfigTypeReq channelConfigTypeReq) {
        PayChannelType payChannelType = payChannelTypeService.getById(channelConfigTypeReq.getId());
        if (payChannelType == null) {
            throw RRException.build("payChannelType.not.exist");
        }
        if (!payChannelType.getCode().equals(channelConfigTypeReq.getCode())) {
            PayChannelTypeVo payChannelTypeVo = getByCode(channelConfigTypeReq.getCode());
            if (payChannelTypeVo != null) {
                throw RRException.build("payChannelType.code.exist");
            }
        }
        PayChannelType targetPayChannelType = new PayChannelType();
        BeanUtil.copyProperties(channelConfigTypeReq, targetPayChannelType);
        payChannelTypeService.updateById(targetPayChannelType);
    }

    @Override
    public void deleteBatchIds(@RequestBody List<Long> ids) {
        LambdaQueryWrapper<PayChannelType> wrapper = new LambdaQueryWrapper();
        wrapper.in(PayChannelType::getId, ids);
        List<PayChannelType> targetDbResultList = payChannelTypeService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            targetDbResultList.forEach(payType -> {
                payType.setDelFlag(SuperEntity.DEFAULT_DELETE_FLAG);
            });
            payChannelTypeService.updateBatchById(targetDbResultList);
        }
    }
}
